package com.li.controller;

import com.alibaba.fastjson.JSONObject;
import com.li.domain.FileAddress;
import com.li.service.FileAddressService;
import com.li.utlis.R;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/fileAddress")
public class FileAddressController {

    @Autowired
    private FileAddressService fileAddressService;

    @RequestMapping("/listPageFileAddress")
    public JSONObject listPageFileAddress(FileAddress fileAddress, Integer page, Integer limit){
        List<FileAddress> list=fileAddressService.listPageFileAddress(fileAddress,page,limit);
        Integer count=fileAddressService.listPageFileAddressCount(fileAddress);
        return R.listPage(list,count);
    }

    @RequestMapping("/saveOrUpdateFileAddress")
    public JSONObject saveOrUpdateFileAddress(FileAddress fileAddress){
        try {
            if (fileAddress.getId()==null){
                fileAddressService.addSystemAddress(fileAddress);
            }else {
                fileAddressService.updateSystemAddress(fileAddress);
            }
        }catch (Exception e){
            return R.getError(fileAddress);
        }
        return R.getSuccess();
    }

    @GetMapping("/editFileAddress/{id}")
    public FileAddress editFileAddress(@PathVariable("id") Integer id){
        return fileAddressService.getById(id);
    }

    @GetMapping("/delFileAddress/{id}")
    public JSONObject delFileAddress(@PathVariable("id") Integer id){
        try{
            boolean flag=fileAddressService.removeById(id);
            if (flag){
                return R.getSuccess();
            }else{
                return R.getError(id);
            }
        }catch (Exception e){
            return R.getError(id);
        }
    }
}















